<template>
  <el-popover placement="top" v-model="visible">
    <div style="font-size: 14px; padding: 4px 4px 16px 30px; position: relative">
      <i class="el-icon-warning" style="color: #E6A23C; font-size: 18px; position: absolute; left: 4px"></i>
      {{(message && message != '') ? message : '确定要删除该记录吗？'}}
    </div>
    <div style="text-align: right; padding: 0 4px 0 4px">
      <el-button @click="visible = false" size="mini">取消</el-button>
      <el-button type="danger" @click="deleteThisRow()" size="mini">确定</el-button>
    </div>
    <el-button slot="reference"  v-bind="Object.assign({},{type:'text'},$attrs)" :disabled="disabled">{{label}}</el-button>
  </el-popover>
</template>

<script>
export default {
  data() {
    return {
      visible: false
    }
  },
  props: {
    deleteUrl: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    message: {
      type: String,
      default: ''
    },
    label:{
      type: String,
      default: '删除'
    },
    rowId: {
      type: String,
      default: ''
    },
    deleteParam: {
      type: Object
    },
    beforeSubmit:{
      type: Function,
      default: ()=> {return true}
    },
    byAction:{
      type: Boolean,
      default: false
    },
    delAction:{
      type: Function,
      default: ()=> {}
    }
  },
  methods: {
    deleteThisRow () {

      const _this = this

      if(!this.beforeSubmit()){
        _this.visible = false
        return
      }


      if(this.byAction){
        this.delAction()
        _this.visible = false
        return
      }


      let data = {}

      if (this.deleteParam) {
        data = this.deleteParam
      } else {
        data.securityId = _this.rowId
      }

      _this.$ajax({
        url: _this.deleteUrl,
        method: 'post',
        data,
        success (res) {
          _this.$message({
            showClose: true,
            message: _this.label + '成功',
            type: 'success'
          })
          _this.$emit('afterOperationDone', res)
            _this.visible = false
        },
        afterAjax() {
          _this.visible = false
        }
      })
    }
  }
}
</script>
